#ifndef SPECTROGRAM_DATA_H
#define SPECTROGRAM_DATA_H

#include "point.h"
#include <qwt_plot_spectrogram.h>

class SpectrogramData: public QwtRasterData
{

private:

    QList<Point> points;
    QVector< QVector<double> > matz;
    int rangMax;
    double minx;
    double miny;

public:

    SpectrogramData( QVector< QVector<double> > matriz,int range, double minimox,double minimoy ):
    QwtRasterData(QwtDoubleRect(620000,8680000, 624000, 8684000))
    {
        matz = matriz;
        minx = minimox;
        miny = minimoy;
        rangMax = range;
    }

    virtual QwtRasterData *copy() const
    {
        return new SpectrogramData(matz,rangMax,minx,miny);
    }

    virtual QwtDoubleInterval range() const
    {
        return QwtDoubleInterval(0.0, rangMax);
    }

    virtual double value(double x, double y) const
    {
        double xredondo = qRound(x);
        double yredondo = qRound(y);

        int a = (int)((xredondo - minx)/6);
        int b = (int)((yredondo - miny)/6);


        if( a < 0 || a > matz.count()-1 || b < 0 || b > matz.at(a).count()-1 ){
            return 0.0;
        }
        else{
                return matz.at(a).at(b);
            }
    }


    double valores(double valueX, double valueY)const
    {
        double xredondo = qRound(valueX);
        double yredondo = qRound(valueY);
        double result;
        double dist;
        double comparadores[3];
        double limiteInferior;
        comparadores[0] = 0;
        double limite = 0;
        double numeradores[3];
        double numerador = 0;
        double denominador = 0;
        double peso[3];
        double max;

        //PROCURA O MAXIMO
        for(int i = 0; i < points.count();i++)
        {
             if(qPow(((points[i].getX())- xredondo),2)+(qPow(((points[i].getY())- yredondo),2))> max)
                max = qPow(((points[i].getX())- xredondo),2)+(qPow(((points[i].getY())- yredondo),2));
        }
        // ENCONTRAR OS 6 PONTOS MAIS PROXIMOS
        for(int j = 0; j < 3; j++)
        {
            limiteInferior = max;
            comparadores[j] = 0;

            for(int i = 0; i < points.count();i++)
            {
                dist = qPow(((points[i].getX())- xredondo),2)+(qPow(((points[i].getY())- yredondo),2));
                if(dist <= limiteInferior && dist > limite)
                {
                    limiteInferior = dist;
                    peso[j] = points[i].getColor();
                }
                comparadores[j] = 1/limiteInferior;
                numeradores[j] = comparadores[j]*peso[j];

                numerador += numeradores[j];
                denominador += comparadores[j];
                limite = limiteInferior;
            }
            return result = numerador/denominador;
        }
    }

};

#endif // SPECTROGRAM_DATA_H
